<?php
include("../includes/DBFunctions.php");
include("../includes/getCredentials.php");

function addSpeciesToRegionFunctor( $sid, $rid, $pop ) {
	$gc = new GetCredentials;
	$uid = $gc->getUserID();
	$db = new DBFunctions;
	$db->connectDB();
	$result = $db->query("call addSpeciesToRegion(".$sid.",".$rid.",".$uid.",".$pop.",@a)" );
	$errors = $db->query("SELECT @a");
	while( $line = mysqli_fetch_array( $errors, MYSQL_ASSOC ) ) {
		$error = $line['@a'];
	}
	if( !$result || $error != 0 ) {
		die("alert('Failed stored procedure call (addSpeciesToRegionFunctor ErrorCode:".$error.")');");
	}
}

if( !isset( $_POST['selectSpecies']) || $_POST['selectSpecies'] == '' ) {
	die("alert('No species was selected. Please try again.');");
}
$sid = $_POST['selectSpecies'];
if( !isset( $_POST['selectRegion']) || $_POST['selectRegion'] == '' ) {
	die("alert('No region was selected. Please try again.');");
}
$rid = $_POST['selectRegion'];
if( !isset( $_POST['population']) || $_POST['population'] == ''  ) {
	die("alert('No population was given. Please try again.');");
}
$pop = $_POST['population'];

addSpeciesToRegionFunctor( $sid, $rid, $pop );
?>